package erfgame.core.world.generator.terrain;

import erfgame.core.world.terrain.TerrainConstants;

public class SlopedTerrainBlockGenerator implements TerrainBlockGenerator {

	private double xangle;
	private double yangle;
	private int zeroZ;
	private int zeroX;
	private byte terrainType;
	
	public SlopedTerrainBlockGenerator( double xangle, double yangle, int zeroX, int zeroZ, byte terrainType ) {
		this.xangle = xangle;
		this.yangle = yangle;
		this.zeroX = zeroX;
		this.zeroZ = zeroZ;
		this.terrainType = terrainType;
	}
	
	public boolean generate(int bx, int by, int bz, int bw, int bh, int bd,
			byte[][][] terrain) {
		double tanx = Math.tan( xangle );
		double tany = Math.tan( yangle );
		for( int x=bw; x>0; ) {
			x--;
			double xz = tanx * (x + bx*bw) - zeroX;
			for( int y=bh; y>0; ) {
				y--;
				double yz = tany * (y + by*bh);
				int startz = Math.max( 0, Math.min(bd, (int)Math.round( xz + yz ) + zeroZ - bz*bd ) );
				for( int z=0; z<startz; z++ ) {
					terrain[x][y][z] = TerrainConstants.TERRAIN_TYPE_AIR;
				}
				for( int z=startz; z<bd; z++ ) {
					terrain[x][y][z] = terrainType;
				}
			}
		}
		return true;
	}

}
